import { contextBridge,ipcRenderer } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'

// Custom APIs for renderer
const api = {
  /**
   * 窗口最小化
   */
  minimizewindow: () => {
    ipcRenderer.send("minimizewindow");
  },
  /**
   * 最大画窗口
   */
  maximizewindow: () => {
    ipcRenderer.send("maximizewindow");
  },
  /**
   * 关闭窗口
   */
  closethewindow: () => {
    ipcRenderer.send("closethewindow");
  },
  /**
   * 打开链接
   * @param url 路由
   */
  openurl: (url: string) => {
    ipcRenderer.send("openurl", url);
  },
  /**
   * 复制内容
   * @param text 文字内容
   */
  copycontent: (text: string) => {
    ipcRenderer.send("copycontent", text);
  },
  /**
   * 窗口移动
   * @param data 移动数据
   */
  mousedown: (data) => {
    ipcRenderer.send("mousedown", data);
  },
  /**
   * 发送一条系统消息
   * @param title 标题
   * @param body 提示内容
   */
  openWindowMessage: (title: string, body: string) => {
    if (body){
      ipcRenderer.send("openWindowMessage", title || "提示",body);
    }
  }}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', electronAPI)
    contextBridge.exposeInMainWorld('api', api)
  } catch (error) {
    console.error(error)
  }
} else {
  // @ts-ignore (define in dts)
  window.electron = electronAPI
  // @ts-ignore (define in dts)
  window.api = api
}
